package com.axend.aerosense.home.viewmodel;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.entity.AlarmListBean;
import com.axend.aerosense.home.entity.GetRoomLogBean;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

public class HomeAlarmViewModel extends CustomBaseViewModel<AlarmListBean> {
    private Disposable disposable;
    private String roomId;
    public int pageIndex = 1;

    public HomeAlarmViewModel(IBaseView baseViewImpl, ILoadDataResult<AlarmListBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
    }

    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }

    @Override
    public void onLoad() {
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(TextUtils.isEmpty(token)){
            return;
        }
        pageIndex = 1;
        if(TextUtils.isEmpty(roomId)){
            disposable = EasyHttp.post(UrlApi.URL.GetAllLog.getUrl())
                    .cacheMode(CacheMode.NO_CACHE)
                    .headers("token", token)
                    .upJson(JsonUtil.toJson(new GetRoomLogBean("",  pageIndex)))
                    .execute(new SimpleCallBack<AlarmListBean>() {
                        @Override
                        public void onTokenExpire() {
                            ToastUtils.showShort(R.string.common_login_first);
                            ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                        }

                        @Override
                        public void onError(ApiException e) {
                            loadDataFail(e.getMessage());
                        }

                        @Override
                        public void onSuccess(AlarmListBean bean) {
                            loadDataSuccess(bean);
                            // 下次加载数据认为是刷新
                            isRefresh = true;
                        }
                    });
        }else{
            disposable = EasyHttp.post(UrlApi.URL.GetRoomLog.getUrl())
                    .cacheMode(CacheMode.NO_CACHE)
                    .headers("token", token)
                    .upJson(JsonUtil.toJson(new GetRoomLogBean(roomId, pageIndex)))
                    .execute(new SimpleCallBack<AlarmListBean>() {
                        @Override
                        public void onTokenExpire() {
                            ToastUtils.showShort(R.string.common_login_first);
                            ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                        }

                        @Override
                        public void onError(ApiException e) {
                            loadDataFail(e.getMessage());
                        }

                        @Override
                        public void onSuccess(AlarmListBean bean) {
                            loadDataSuccess(bean);
                            // 下次加载数据认为是刷新
                            isRefresh = true;
                        }
                    });
        }

    }

    public void getMoreData(int page, IOperationDetailResult<AlarmListBean> operationDetailResult){
        if(TextUtils.isEmpty(roomId)){
            getHomeAlarmMore(page, operationDetailResult);
        }else{
            getRoomAlarmMore(page, operationDetailResult);
        }
    }

    private void getHomeAlarmMore(int page, IOperationDetailResult<AlarmListBean> operationDetailResult){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(TextUtils.isEmpty(token)){
            return;
        }
        disposable = EasyHttp.post(UrlApi.URL.GetAllLog.getUrl())
                .cacheMode(CacheMode.NO_CACHE)
                .headers("token", token)
                .upJson(JsonUtil.toJson(new GetRoomLogBean("", page)))
                .execute(new SimpleCallBack<AlarmListBean>() {
                    @Override
                    public void onTokenExpire() {
                        if(null != operationDetailResult){
                            operationDetailResult.onFail(Gobal.ERROR_CODE.ERR_LOGIN, "");
                        }
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        if(null != operationDetailResult){
                            operationDetailResult.onFail(Gobal.ERROR_CODE.ERR_COMMON, e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(AlarmListBean bean) {
                        if(null != operationDetailResult){
                            operationDetailResult.onSuccess(bean);
                        }
                    }
                });
    }

    private void getRoomAlarmMore(int page, IOperationDetailResult<AlarmListBean> operationDetailResult){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(TextUtils.isEmpty(token) || TextUtils.isEmpty(roomId)){
            return;
        }
        disposable = EasyHttp.post(UrlApi.URL.GetRoomLog.getUrl())
                .cacheMode(CacheMode.NO_CACHE)
                .headers("token", token)
                .upJson(JsonUtil.toJson(new GetRoomLogBean(roomId,  page)))
                .execute(new SimpleCallBack<AlarmListBean>() {
                    @Override
                    public void onTokenExpire() {
                        if(null != operationDetailResult){
                            operationDetailResult.onFail(Gobal.ERROR_CODE.ERR_LOGIN, "");
                        }
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        if(null != operationDetailResult){
                            operationDetailResult.onFail(Gobal.ERROR_CODE.ERR_COMMON, e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(AlarmListBean bean) {
                        if(null != operationDetailResult){
                            operationDetailResult.onSuccess(bean);
                        }
                    }
                });
    }



    @Override
    protected void onCleared() {
        if (null != disposable && !disposable.isDisposed()){
            disposable.dispose();
        }
        super.onCleared();
    }
}
